home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / asm / adisv1_3.lha / src / disasm_data.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-20  |  4.9 KB  |  195 lines

  1. /*
  2.  * Change history
  3.  * $Log:    disasm_data.c,v $
  4.  * Revision 3.0  93/09/24  17:53:54  Martin_Apel
  5.  * New feature: Added extra 68040 FPU opcodes
  6.  * 
  7.  * Revision 2.2  93/07/18  22:55:49  Martin_Apel
  8.  * *** empty log message ***
  9.  * 
  10.  * Revision 2.1  93/07/10  13:02:02  Martin_Apel
  11.  * Major mod.: Added full jump table support. Seems to work quite well
  12.  * 
  13.  * Revision 2.0  93/07/01  11:53:52  Martin_Apel
  14.  * *** empty log message ***
  15.  * 
  16.  * Revision 1.17  93/07/01  11:39:28  Martin_Apel
  17.  * Minor mod.: Removed dependance on ctype.h
  18.  * 
  19.  * Revision 1.16  93/06/06  00:10:55  Martin_Apel
  20.  * Bug fix: DC.B $80 was printed as DC.B -$7F
  21.  * 
  22.  * Revision 1.15  93/06/03  20:26:58  Martin_Apel
  23.  * Minor mod.: Additional linefeed generation for end instructions has been
  24.  *             moved to format_line
  25.  * 
  26.  * Revision 1.14  93/06/03  18:29:49  Martin_Apel
  27.  * Minor mod.: Addressing relative to hunk end changed
  28.  * 
  29.  */
  30.  
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include "defs.h"
  34.  
  35. static char rcsid [] = "$Id: disasm_data.c,v 3.0 93/09/24 17:53:54 Martin_Apel Exp $";
  36.  
  37. void disasm_data (UBYTE *data_seg, ULONG seg_size)
  38.  
  39. {
  40. /* data_seg is guaranteed to be an even address */
  41.  
  42. register char *data_ptr;
  43. char *seg_end;
  44. ULONG offset;
  45. char *next_label,
  46.      *last_label;
  47. UWORD dummy_access;
  48. ULONG size;
  49. BOOL first_time = TRUE;
  50. BOOL valid_string;
  51. ULONG last_address_printed;
  52. ULONG tmp_offset;
  53.  
  54. seg_end = (char*)data_seg + seg_size;
  55. data_ptr = (char*)data_seg;
  56. offset = current_address - first_address;
  57. last_address_printed = current_address;
  58. dest [0] = 0;
  59. end_instr = FALSE;
  60.  
  61. next_label = data_ptr;
  62. while (data_ptr < seg_end)
  63.   {
  64.   /* find next label */
  65.   last_label = next_label;
  66.   next_label = data_ptr - current_address +
  67.             next_reference (current_address, last_address, &dummy_access);
  68.   if (next_label > seg_end)
  69.     next_label = seg_end;
  70.  
  71.   /* Search from data_ptr forward to next_label until stepping onto
  72.      a relocated address. If found, mark this as the next label.
  73.      This way no strings will cross relocated addresses */
  74.   if (IS_RELOCATED (current_address))
  75.     tmp_offset = 4;
  76.   else
  77.     tmp_offset = 2;
  78.   for (; data_ptr + tmp_offset < next_label; tmp_offset++)
  79.     {
  80.     if (IS_RELOCATED (current_address + tmp_offset))
  81.       {
  82.       next_label = data_ptr + tmp_offset;
  83.       break;
  84.       }
  85.     }
  86.  
  87.   while (data_ptr < next_label)
  88.     {
  89.     if (verbose && (current_address - last_address_printed > 0x200))
  90.       {
  91.       printf (">%5lx\r", current_address);
  92.       fflush (stdout);
  93.       last_address_printed = current_address;
  94.       }
  95.  
  96.     valid_string = is_string (data_ptr, next_label - data_ptr);
  97.  
  98.     if (!ODD (offset) && IS_RELOCATED (current_address))
  99.       {
  100.        /* The long word starting at this address is relocated */
  101.       strcpy (opcode, "DC.L");
  102.       gen_label (src, *((ULONG*)data_ptr), TRUE);
  103.       size = 4;
  104.       }
  105.     else if (!ODD (offset) && (code = (unsigned short*)data_ptr,
  106.                                find_jmptab_and_print (src)))
  107.       {
  108.       strcpy (opcode, "DC.W");
  109.       size = 2;
  110.       }
  111.     else if (!ODD (offset) && (next_label - data_ptr == 4) &&
  112.              (data_ptr == last_label) && !valid_string)
  113.       {
  114.       /* It seems to be a longword */
  115.       strcpy (opcode, "DC.L");
  116.       format_ld (src, *((ULONG*)data_ptr), TRUE);
  117.       size = 4;
  118.       }
  119.     else if (valid_string)
  120.       {
  121.       char *to_be_copied;
  122.       
  123.       strcpy (opcode, "DC.B");
  124.       to_be_copied = cpstr (src, data_ptr, 75 - PARAM_COL);
  125.       if (to_be_copied == NULL)
  126.         size = strlen (data_ptr) + 1;
  127.       else
  128.         size = to_be_copied - data_ptr;
  129.       }
  130.     else if ((current_address & 1) == 0 && (data_ptr + 1 != next_label))
  131.       {    
  132.       strcpy (opcode, "DC.W");
  133.       format_d (src, *(UWORD*)data_ptr, TRUE);
  134.       size = 2;
  135.       }
  136.     else  /* write a single byte */
  137.       {
  138.       strcpy (opcode, "DC.B");
  139.       format_d (src, (UBYTE)*data_ptr, TRUE);
  140.       size = 1;
  141.       }
  142.  
  143.     format_line (first_time, TRUE);
  144.     first_time = FALSE;
  145.     put (instruction);
  146.     current_address += size;
  147.     data_ptr += size;
  148.     offset += size;
  149.     }
  150.   }
  151. }
  152.  
  153.  
  154. void disasm_bss ()
  155.  
  156. {
  157. ULONG seg_end;
  158. ULONG next_label;
  159. UWORD dummy_access;
  160.  
  161. seg_end = *(hunk_end + current_hunk);
  162. end_instr = FALSE;
  163.  
  164. while (current_address < seg_end)
  165.   {
  166.   next_label = next_reference (current_address, last_address, &dummy_access);
  167.  
  168.   if (((current_address & 1) == 0) && 
  169.       (((next_label - current_address) & 0x3) == 0))
  170.     {
  171.     /* Multiple of 4 bytes */
  172.     strcpy (opcode, "DS.L");
  173.     format_d (src, (UWORD)((next_label - current_address) / 4), FALSE);
  174.     }
  175.   else if (((current_address & 1) == 0) && 
  176.       (((next_label - current_address) & 0x1) == 0))
  177.     {
  178.     /* Multiple of 2 bytes */
  179.     strcpy (opcode, "DS.W");
  180.     format_d (src, (UWORD)((next_label - current_address) / 2), FALSE);
  181.     }
  182.   else  /* write a single byte */
  183.     {
  184.     strcpy (opcode, "DS.B");
  185.     format_d (src, (UWORD)(next_label - current_address), FALSE);
  186.     }
  187.  
  188.   format_line (FALSE, FALSE);
  189.   put (instruction);
  190.   current_address = next_label;  
  191.   }
  192.  
  193. put ("\n");
  194. }
  195.